package physics.collision;

import java.awt.geom.Point2D;
import model.objects.GameObject;
import model.objects.Level;


/**
 * Defines the collision strategy for solid objects. These are objects that do
 * not move during a collision, but may cause other objects to stop on impact
 * (e.g. a brick).
 * 
 * @author geo4
 * 
 */
public class SolidStrategy implements CollisionStrategy {

    @Override
    public void collisionAction (Level level, CollisionEvent collisionEvent) {
        // In this case the "impactedObject" is whatever ran into the solid
        // object
        GameObject solidObject = (GameObject) collisionEvent.getCollidingObject();
        GameObject impactingObject = (GameObject) collisionEvent.getImpactedObject();
        Point2D impactedObjectVelocity = impactingObject.getVelocity();

        if (collisionEvent.collided(CollisionEvent.Direction.TOP)) {
            impactedObjectVelocity.setLocation(impactedObjectVelocity.getX(), 0);
            impactingObject.setPosition(impactingObject.getBounds().getX(), solidObject.getBounds()
                    .getY() - impactingObject.getBounds().getHeight() - Double.MIN_VALUE);
            impactingObject.setOnGround(true);
        }
        else if (collisionEvent.collided(CollisionEvent.Direction.BOTTOM)) {
            impactedObjectVelocity.setLocation(impactedObjectVelocity.getX(), 0);
            impactingObject.setPosition(impactingObject.getBounds().getX(), solidObject.getBounds()
                    .getY() + solidObject.getBounds().getHeight() + Double.MIN_VALUE);
        }
        else if (collisionEvent.collided(CollisionEvent.Direction.LEFT)) {
            impactedObjectVelocity.setLocation(0, impactedObjectVelocity.getY());
            impactingObject.setPosition(solidObject.getBounds().getX() -
                                                impactingObject.getBounds().getWidth() -
                                                Double.MIN_VALUE,
                                        impactingObject.getBounds().getY());
        }
        else if (collisionEvent.collided(CollisionEvent.Direction.RIGHT)) {
            impactedObjectVelocity.setLocation(0, impactedObjectVelocity.getY());
            impactingObject.setPosition(solidObject.getBounds().getWidth() +
                                                impactingObject.getBounds().getX() +
                                                Double.MIN_VALUE,
                                        impactingObject.getBounds().getY());
        }
    }

}
